package com.newtouch.cq.base.starter.business.controller;


import com.newtouch.cq.base.starter.business.entity.SysRole;
import com.newtouch.cq.base.starter.business.model.dto.SysRoleAddDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysRoleMenuDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysRolePageDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysRoleUpdDTO;
import com.newtouch.cq.base.starter.business.service.IBaseSysRoleService;
import com.newtouch.cq.lang.annotation.NoLog;
import com.newtouch.cq.lang.result.MccObjectResult;
import com.newtouch.cq.lang.result.MccPageResult;
import com.newtouch.cq.lang.result.MccResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/sys/roles")
@Api(tags = "角色相关接口")
public class BaseSysRoleController {

    @Autowired
    private IBaseSysRoleService roleService;

    /**
     * 新增角色
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增角色")
    @PreAuthorize("hasAuthority('sys_role_add')")
    public MccResult add(@RequestBody SysRoleAddDTO dto) {
        roleService.add(dto);
        return MccResult.SUCCESS;
    }

    /**
     * 角色编辑
     * @param id
     * @param dto
     * @return
     */
    @ApiOperation("角色编辑")
    @PutMapping("{id}")
    @PreAuthorize("hasAuthority('sys_role_upd')")
    @ApiImplicitParam(name = "id", value = "角色标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult update(@PathVariable long id,@RequestBody SysRoleUpdDTO dto) {
        roleService.update(id,dto);
        return MccResult.SUCCESS;
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @NoLog
    @ApiOperation("分页查询")
    @GetMapping("pageList")
    public MccPageResult<SysRole> pageList(SysRolePageDTO dto) {
        return roleService.pageList(dto);
    }

    /**
     * 获取角色详情
     * @param id
     * @return
     */
    @ApiOperation("获取角色详情")
    @GetMapping("{id}")
    @ApiImplicitParam(name = "id", value = "角色标识", dataTypeClass = Long.class,paramType = "path")
    public MccObjectResult<SysRole> get(@PathVariable long id) {
        return MccObjectResult.success(roleService.getById(id));
    }

    /**
     * 更新角色菜单
     *
     * @return success、false
     */
    @ApiOperation("更新角色菜单")
    @PutMapping("/roleMenuUpd")
    public MccResult roleMenuUpd(@RequestBody SysRoleMenuDTO dto) {
        roleService.roleMenuUpd(dto);
        return MccResult.SUCCESS;
    }


    /**
     * 获取角色列表
     * @return
     */
    @NoLog
    @ApiOperation("获取角色列表")
    @GetMapping
    public MccObjectResult<List<SysRole>> roleList() {
        return MccObjectResult.success(roleService.enableList());
    }

    /**
     * 通过用户标识查询角色
     * @param userId
     * @return
     */
    @NoLog
    @ApiOperation("通过用户标识查询角色")
    @GetMapping("getUserRole/{userId}")
    @ApiImplicitParam(name = "userId", value = "用户标识", dataTypeClass = Long.class,paramType = "path")
    public MccObjectResult<List<SysRole>> userRole(@PathVariable long userId) {
        return MccObjectResult.success(roleService.userRole(userId));
    }

    /**
     * 角色删除
     * @param id
     * @return
     */
    @ApiOperation("角色删除")
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('sys_role_del')")
    @ApiImplicitParam(name = "id", value = "角色标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult roleDel(@PathVariable long id) {
        roleService.roleDel(id);
        return MccResult.SUCCESS;
    }
}
